﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GridManager{

    private static GridManager mInstance;
    public static int mGridListLayer = 12;
    public static int mGridListLayerMask = 1<<mGridListLayer;
    public static int mGroundLayer = 13;
    public static int mGroundLayerMask = 1<<mGroundLayer;
    public static int mWallLayer = 14;
    public static int mWallLayerMask = 1<<mWallLayer;
    public static int mGoodsLayer = 15;
    public static int mGoodsLayerMask = 1<<mGoodsLayer;

    protected int mGridSize = 500;
    protected GridData[,] gridList;
    public GameObject MyGroundBox;
    protected GameObject MyGroundBoxSize;
    protected int MyGroundLeft = 0;
    protected int MyGroundRight = 0;
    protected int MyGroundTop = 0;
    protected int MyGroundBottom = 0;
    protected int MyGroundSizeX = 0;
    protected int MyGroundSizeY = 0;
    protected Collider MyGroundBoxCollider;
    //单例
    public static GridManager GetInstance()
    {
        if (mInstance == null)
        {
            mInstance = new GridManager();
        }
        return mInstance;
    }
    private GridManager()
    {
        mInstance = this;
    }

    public void SetMapSize(int nMapSize)
    {
		MyGroundBox = GameObject.Find("MyGroundBox");
        MyGroundBoxSize = GameObject.Find("MyGroundBoxSize");

        int oldSize = mGridSize;
        mGridSize = nMapSize;
        if(oldSize > 0 && gridList != null)
        {
            GridData[,] oldGridList = gridList;
            gridList = new GridData[mGridSize, mGridSize];
            if(oldSize > mGridSize)
            {
                for (int y = 0; y < mGridSize; y++)
                {
                    for (int x = 0; x < mGridSize; x++)
                    {
                        gridList[y, x] = oldGridList[y,x];
                    }
                    for(int x = mGridSize; x < oldSize; x++)
                    {
                        oldGridList[y, x].Clean();
                    }
                }
                for (int y = mGridSize; y < oldSize; y++)
                {
                    for (int x = 0; x < oldSize; x++)
                    {
                        oldGridList[y, x].Clean();
                    }
                }
            }
            else
            {
                for (int y = 0; y < mGridSize; y++)
                {
                    if(y < oldSize)
                    {
                        for (int x = 0; x < mGridSize; x++)
                        {
                            if(x < oldSize)
                            {
                                gridList[y, x] = oldGridList[y,x];
                            }
                            else
                            {                                
                                gridList[y, x] = new GridData(x, y);
                                gridList[y, x].Clean();
                            }
                        }
                    }
                    else
                    {
                        for (int x = 0; x < mGridSize; x++)
                        {
                            gridList[y, x] = new GridData(x, y);
                            gridList[y, x].Clean();
                        }
                    }
                }
            }
        }
        else
        {
            gridList = new GridData[mGridSize, mGridSize];
            for (int y = 0; y < mGridSize; y++)
            {
                for (int x = 0; x < mGridSize; x++)
                {
                    gridList[y, x] = new GridData(x, y);
                    gridList[y, x].Clean();
                }
            }
        }
        
        MyGroundLeft = mGridSize/2;
        MyGroundRight = mGridSize/2;
        MyGroundTop = mGridSize/2;
        MyGroundBottom = mGridSize/2;
        MyGroundSizeX = 0;
        MyGroundSizeY = 0;
    }

    public void ClearGrid()
    {
        for (int y = 0; y < mGridSize; y++)
        {
            for (int x = 0; x < mGridSize; x++)
            {
                gridList[y, x].Clean();
            }
        }
        
        MyGroundLeft = mGridSize/2;
        MyGroundRight = mGridSize/2;
        MyGroundTop = mGridSize/2;
        MyGroundBottom = mGridSize/2;
        MyGroundSizeX = 0;
        MyGroundSizeY = 0;
    }

    public WallData GetWallFile(string modelName, int nIndex)
    {
        WallData wallData = new WallData();
        wallData.nRotate = 0;
        uint nCount = DBCManager.GetInstance().GetWallDBC().GetRecordCount( );
        for(uint i = 0; i < nCount; i++)
        {
            string tempName = DBCManager.GetInstance().GetWallDBC().GetString(i, 0);
            if(tempName == modelName)
            {
                string modelFile = DBCManager.GetInstance().GetWallDBC().GetString(i, 1);
                string type = DBCManager.GetInstance().GetWallDBC().GetString(i, 2);
                uint nTypeCount = DBCManager.GetInstance().GetWallTypeDBC().GetRecordCount();
                for(uint y = 0; y < nTypeCount; y++)
                {
                     if(DBCManager.GetInstance().GetWallTypeDBC().GetString(y, 0) == type)
                    {
                        string indexStr = DBCManager.GetInstance().GetWallTypeDBC().GetString(y, (uint)(nIndex+1));
                        int nPos = indexStr.IndexOf(':');
                        int index = 0;
                        if(nPos != -1)
                        {
                            index = int.Parse(indexStr.Substring(0, nPos));
                            wallData.nRotate = int.Parse(indexStr.Substring(nPos+1));
                        }
                        else
                        {
                            index = int.Parse(indexStr);
                        }
                        wallData.fileName = modelFile+"_"+index.ToString();
                        break;
                    }
                }                
                break;
            }
        }
        return wallData;
    }
    public string GetWoodenWallData(string modelName)
    {
        uint nCount = DBCManager.GetInstance().GetWoodenWallDBC().GetRecordCount( );
        for(uint i = 0; i < nCount; i++)
        {
            string tempName = DBCManager.GetInstance().GetWoodenWallDBC().GetString(i, 0);
            if(tempName == modelName)
            {
                return DBCManager.GetInstance().GetWoodenWallDBC().GetString(i, 1);
            }
        }
        
        return "";
    }

    public OtherData GetOtherData(string modelName)
    {
        OtherData otherData = new OtherData();
        uint nCount = DBCManager.GetInstance().GetOtherDBC().GetRecordCount( );
        for(uint i = 0; i < nCount; i++)
        {
            string tempName = DBCManager.GetInstance().GetOtherDBC().GetString(i, 0);
            if(tempName == modelName)
            {
                otherData.fileName = DBCManager.GetInstance().GetOtherDBC().GetString(i, 1);
                otherData.nWidth = DBCManager.GetInstance().GetOtherDBC().GetInt(i, 2);
                otherData.nHeight = DBCManager.GetInstance().GetOtherDBC().GetInt(i, 3);
                break;
            }
        }
        
        return otherData;
    }

    public string GetGroundData(string modelName)
    {
        uint nCount = DBCManager.GetInstance().GetGroundDBC().GetRecordCount( );
        for(uint i = 0; i < nCount; i++)
        {
            string tempName = DBCManager.GetInstance().GetGroundDBC().GetString(i, 0);
            if(tempName == modelName)
            {
                return DBCManager.GetInstance().GetGroundDBC().GetString(i, 1);
            }
        }
        
        return "";
    }

    public void UpdateGroundSize()
    {        
        MyGroundLeft = mGridSize;
        MyGroundRight = 0;
        MyGroundTop = mGridSize;
        MyGroundBottom = 0;
        MyGroundSizeX = 0;
        MyGroundSizeY = 0;        
        for (int y = 0; y < mGridSize; y++)
        {
            for (int x = 0; x < mGridSize; x++)
            {
                if(gridList[y, x].groundName != "")
                {
                    if(x < MyGroundLeft)
                    {
                        MyGroundLeft = x;
                    }
                    if(x > MyGroundRight)
                    {
                        MyGroundRight = x;
                    }
                    if(y < MyGroundTop)
                    {
                        MyGroundTop = y;
                    }
                    if(y > MyGroundBottom)
                    {
                        MyGroundBottom = y;
                    }
                }
            }
        }
        if(MyGroundBoxSize != null && MyGroundRight >= MyGroundLeft && MyGroundBottom >= MyGroundTop)
        {
            MyGroundSizeX = MyGroundRight-MyGroundLeft+1;
            MyGroundSizeY = MyGroundBottom-MyGroundTop+1;
            Vector3 pos = new Vector3();
            pos.x = (MyGroundLeft+MyGroundRight)*0.5f+1;
            pos.y = MyGroundBoxSize.transform.localPosition.y;
            pos.z = (MyGroundTop+MyGroundBottom)*0.5f+1;

            Vector3 zoom = new Vector3();
            zoom.x = MyGroundSizeX;
            zoom.y = 1.0f;
            zoom.z = MyGroundSizeY;

            MyGroundBoxSize.transform.localPosition = pos;
            MyGroundBoxSize.transform.localScale = zoom;
        }
    }

    public void AddGround(string objName, int x, int y, int rotate = 0)
    {
        if(x < 0 || x >= mGridSize || y < 0 || y >= mGridSize)
        {
            return;
        }
        gridList[y, x].SetGround(objName, rotate);
        UpdateGroundSize();
    }
    public void DelGround(int x, int y)
    {
        gridList[y, x].CleanGround();
        UpdateGroundSize();
    }

    public bool CanAddObj(ObjType objType, int width, int height, int x, int y, int rotate = 0)
    {
        if(x < 0 || x+width >= mGridSize || y < 0 || y+height >= mGridSize)
        {
            return false;
        }
        switch(objType)
        {
            case ObjType.eot_Other:
            {
                for(int h = 0; h < height; h++)
                {
                    for(int w = 0; w < width; w++)
                    {
                        if(gridList[y+h, x+w].objType != ObjType.eot_None)
                        {
                            return false;
                        }
                    }
                }
            }
            break;
            case ObjType.eot_Wall:
            case ObjType.eot_Mine:
            {
                if(gridList[y, x].objType != ObjType.eot_None)
                {
                    return false;
                }
            }
            break;
            case ObjType.eot_WoodenWall:
            {
                switch(gridList[y, x].objType)
                {
                    case ObjType.eot_None:
                    case ObjType.eot_Assist:
                    {
                    }
                    break;
                    case ObjType.eot_Other:
                    case ObjType.eot_Mine:
                    case ObjType.eot_Wall:
                    {
                        return false;
                    }
                    case ObjType.eot_WoodenWall:
                    {
                        if(rotate == 0 || rotate == 2)
                        {
                            if(gridList[y, x].obj != null)
                            {
                                return false;
                            }
                        }
                        if(gridList[y, x].objY != null)
                        {
                            return false;
                        }
                    }
                    break;
                }
                if(rotate == 0 || rotate == 2)
                {
                    if(gridList[y, x+1].objType != ObjType.eot_None && gridList[y, x+1].objType != ObjType.eot_Assist && gridList[y, x+1].objType != ObjType.eot_WoodenWall)
                    {
                        return false;
                    }
                }
                else
                {
                    if(gridList[y+1, x].objType != ObjType.eot_None && gridList[y+1, x].objType != ObjType.eot_Assist && gridList[y+1, x].objType != ObjType.eot_WoodenWall)
                    {
                        return false;
                    }
                }
            }
            break;
        }
        return true;
    }
    public bool AddBuilding(ObjType objType, string objName, int x, int y, int rotate = 0)
    {        
        switch(objType)
        {
            case ObjType.eot_Wall:
            case ObjType.eot_Mine:
            {
                if(x < 0 || x >= mGridSize || y < 0 || y >= mGridSize)
                {
                    return false;
                }
                if(gridList[y, x].objType != ObjType.eot_None)
                {
                    return false;
                }
                gridList[y, x].SetData(objType, objName, rotate);
                return ChangeWall(x, y);
            }
            case ObjType.eot_WoodenWall:
            {
                if(x < 0 || x+1 >= mGridSize || y < 0 || y+1 >= mGridSize)
                {
                    return false;
                }
                
                switch(gridList[y, x].objType)
                {
                    case ObjType.eot_None:
                    case ObjType.eot_Assist:
                    {
                    }
                    break;
                    case ObjType.eot_Other:
                    case ObjType.eot_Mine:
                    case ObjType.eot_Wall:
                    {
                        return false;
                    }
                    case ObjType.eot_WoodenWall:
                    {
                        if(rotate == 0 || rotate == 2)
                        {
                            if(gridList[y, x].obj != null)
                            {
                                return false;
                            }
                        }
                        else if(gridList[y, x].objY != null)
                        {
                            return false;
                        }
                    }
                    break;
                }
                if(rotate == 0 || rotate == 2)
                {
                    if(gridList[y, x+1].objType != ObjType.eot_None && gridList[y, x+1].objType != ObjType.eot_Assist && gridList[y, x+1].objType != ObjType.eot_WoodenWall)
                    {
                        return false;
                    }
                }
                else
                {
                    if(gridList[y+1, x].objType != ObjType.eot_None && gridList[y+1, x].objType != ObjType.eot_Assist && gridList[y+1, x].objType != ObjType.eot_WoodenWall)
                    {
                        return false;
                    }
                }
                if(rotate == 0 || rotate == 2)
                {
                    gridList[y, x].SetData(objType, objName, rotate);
                    if(gridList[y, x+1].objType == ObjType.eot_None)
                    {
                        gridList[y, x+1].SetData(ObjType.eot_Assist, "", 0);
                    }
                }
                else
                {
                    gridList[y, x].SetDataY(objType, objName, rotate);
                     if(gridList[y+1, x].objType == ObjType.eot_None)
                    {
                        gridList[y+1, x].SetData(ObjType.eot_Assist, "", 0);
                    }
                }
                gridList[y, x].UpdateModel();
                return true;
            }
            case ObjType.eot_Other:
            {
                OtherData otherData = GridManager.GetInstance().GetOtherData(objName);
                if(otherData.nWidth > 1 || otherData.nHeight > 1)
                {
                    if(otherData.nWidth != otherData.nHeight && rotate != 0 && rotate != 2)
                    {// 旋转
                        int nLeft = (int)((otherData.nHeight-1)*0.5);
                        int nRight = otherData.nHeight-nLeft;
                        int nTop = (int)((otherData.nWidth-1)*0.5);
                        int nBottom = otherData.nWidth-nTop;
                        for(int iy = -nTop; iy < nBottom; iy++)
                        {
                            for(int ix = -nLeft; ix < nRight; ix++)
                            {
                                if(gridList[y+iy, x+ix].objType != ObjType.eot_None && gridList[y+iy, x+ix].objType != ObjType.eot_Assist)
                                {
                                    return false;
                                }
                            }
                        }
                        
                        for(int iy = -nTop; iy < nBottom; iy++)
                        {
                            for(int ix = -nLeft; ix < nRight; ix++)
                            {
                                gridList[y+iy, x+ix].nearList = new GridData[otherData.nHeight*otherData.nWidth-1];
                                gridList[y+iy, x+ix].SetData(objType, objName, rotate);
                                int nIndex = 0;
                                for(int iyt = -nTop; iyt < nBottom; iyt++)
                                {
                                    for(int ixt = -nLeft; ixt < nRight; ixt++)
                                    {
                                        if(iyt != iy || ixt != ix)
                                        {
                                            gridList[y+iy, x+ix].nearList[nIndex++] = gridList[y+iyt, x+ixt];
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        int nLeft = (int)((otherData.nWidth-1)*0.5);
                        int nRight = otherData.nWidth-nLeft;
                        int nTop = (int)((otherData.nHeight-1)*0.5);
                        int nBottom = otherData.nHeight-nTop;
                        if(x-nLeft < 0 || x+nRight >= mGridSize || y-nTop < 0 || y+nBottom >= mGridSize)
                        {
                            return false;
                        }
                        for(int iy = -nTop; iy < nBottom; iy++)
                        {
                            for(int ix = -nLeft; ix < nRight; ix++)
                            {
                                if(gridList[y+iy, x+ix].objType != ObjType.eot_None && gridList[y+iy, x+ix].objType != ObjType.eot_Assist)
                                {
                                    return false;
                                }
                            }
                        }
                        
                        for(int iy = -nTop; iy < nBottom; iy++)
                        {
                            for(int ix = -nLeft; ix < nRight; ix++)
                            {
                                gridList[y+iy, x+ix].nearList = new GridData[otherData.nHeight*otherData.nWidth-1];
                                gridList[y+iy, x+ix].SetData(objType, objName, rotate);
                                int nIndex = 0;
                                for(int iyt = -nTop; iyt < nBottom; iyt++)
                                {
                                    for(int ixt = -nLeft; ixt < nRight; ixt++)
                                    {
                                        if(iyt != iy && ixt != ix)
                                        {
                                            gridList[y+iy, x+ix].nearList[nIndex++] = gridList[y+iyt, x+ixt];
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    if(x < 0 || x >= mGridSize || y < 0 || y >= mGridSize)
                    {
                        return false;
                    }
                    if(gridList[y, x].objType != ObjType.eot_None && gridList[y, x].objType != ObjType.eot_Assist)
                    {
                        return false;
                    }
                    gridList[y, x].SetData(objType, objName, rotate);
                }
                gridList[y, x].UpdateModel();
                return true;
            }
        }
        return false;
    }
    public bool DelBuilding(int x, int y, int nIndex = 0)
    {
        if(x < 0 || x >= mGridSize || y < 0 || y >= mGridSize)
        {
            return false;
        }
        if(gridList[y, x].objType == ObjType.eot_None || gridList[y, x].objType == ObjType.eot_Assist)
        {
            return false;
        }
        gridList[y, x].DelObj(nIndex);
        
        return false;
    }

    public void UpdateGridState(int x, int y)
    {        
        gridList[y, x].nState = 0;
        // 上
        if(y > 0)
        {
            int tempY = y-1;
            // 左上
            if(x > 0)
            {
                int tempX = x-1;
                if(gridList[tempY, tempX].objType != ObjType.eot_None && gridList[tempY, tempX].isBuilding)
                {
                    gridList[y, x].nState |= 1;
                }
            }
            if(gridList[tempY, x].objType != ObjType.eot_None && gridList[tempY, x].isBuilding)
            {
                gridList[y, x].nState |= 2;
            }
            // 右上
            if(x < mGridSize-1)
            {
                int tempX = x+1;
                if(gridList[tempY, tempX].objType != ObjType.eot_None && gridList[tempY, tempX].isBuilding)
                {
                    gridList[y, x].nState |= 4;
                }
            }
        }
        // 左
        if(x > 0)
        {
            int temp = x-1;
            if(gridList[y, temp].objType != ObjType.eot_None && gridList[y, temp].isBuilding)
            {
                gridList[y, x].nState |= 8;
            }
        }
        // 右
        if(x < mGridSize-1)
        {
            int temp = x+1;
            if(gridList[y, temp].objType != ObjType.eot_None && gridList[y, temp].isBuilding)
            {
                gridList[y, x].nState |= 16;
            }
        }
        // 下
        if(y < mGridSize-1)
        {
            int tempY = y+1;
            // 左下
            if(x > 0)
            {
                int tempX = x-1;
                if(gridList[tempY, tempX].objType != ObjType.eot_None && gridList[tempY, tempX].isBuilding)
                {
                    gridList[y, x].nState |= 32;
                }
            }
            if(gridList[tempY, x].objType != ObjType.eot_None && gridList[tempY, x].isBuilding)
            {
                gridList[y, x].nState |= 64;
            }
            // 右下
            if(x < mGridSize-1)
            {
                int tempX = x+1;
                if(gridList[tempY, tempX].objType != ObjType.eot_None && gridList[tempY, tempX].isBuilding)
                {
                    gridList[y, x].nState |= 128;
                }
            }
        }
    }

    public void UpdateAssist(int x, int y)
    {
        if(gridList[y, x].objType == ObjType.eot_Assist)
        {
           if(( x <= 0 || gridList[y, x-1].objType != ObjType.eot_WoodenWall || (gridList[y, x-1].objName == "")) &&
                (y <= 0 || gridList[y-1, x].objType != ObjType.eot_WoodenWall || (gridList[y-1, x].objNameY == "")))
            {
                gridList[y, x].objType = ObjType.eot_None;
            }
        }
        else if(gridList[y, x].objType == ObjType.eot_None)
        {
           if(( x > 0 && gridList[y, x-1].objType == ObjType.eot_WoodenWall && (gridList[y, x-1].objName != "")) ||
                (y > 0 && gridList[y-1, x].objType == ObjType.eot_WoodenWall && (gridList[y-1, x].objNameY != "")))
            {
                gridList[y, x].objType = ObjType.eot_Assist;
            }
        }
    }

    public bool ChangeWall(int x, int y, bool bOther = true)
    {// 整理
        UpdateGridState(x, y);
        if(bOther)
        {
            // 上
            if(y > 0)
            {
                // 左上
                if(x > 0) ChangeWall(x-1, y-1, false);
                ChangeWall(x, y-1, false);
                // 右上
                if(x < mGridSize-1) ChangeWall(x+1, y-1, false);
            } 
            // 左
            if(x > 0) ChangeWall(x-1, y, false);
            // 右
            if(x < mGridSize-1) ChangeWall(x+1, y, false);
            // 下
            if(y < mGridSize-1)
            {
                // 左上
                if(x > 0) ChangeWall(x-1, y+1, false);
                ChangeWall(x, y+1, false);
                // 右上
                if(x < mGridSize-1) ChangeWall(x+1, y+1, false);
            } 
        }
        gridList[y, x].UpdateModel();
        return true;
    }
}

public enum ObjType
{ 
    eot_None,
    eot_Other,
    eot_Mine = 0x8001,   // 矿
    eot_Wall = 0x8002,   // 墙
    eot_Assist = 0x4000,   // 辅助格（由于墙偏移0.5格，因此需要辅助格协调）
    eot_WoodenWall = 0x4001, // 居中单模型木墙
}

public struct WallData
{
    public string fileName;
    public int nRotate;
}

public struct OtherData
{
    public string fileName;
    public int nWidth;
    public int nHeight;
}

public class GridData
{
    public static Quaternion[] s_Quaternion = {new Quaternion(0, 0, 0, 1), new Quaternion(0, 0.71f, 0, -0.71f), new Quaternion(0,1,0, 0), new Quaternion(0, 0.71f, 0, 0.71f)};
    public ObjType objType;

    public bool isBuilding;
    public int nState;
    public string objName;
    public string modelFile;
    public WallData curWallData = new WallData();
    public GameObject obj;
    public int x, y;
    public int rotate;
    public string objNameY;
    public string modelFileY;
    public GameObject objY;
    public int rotateY;
    public GridData[] nearList; // 关联格:删除时，一起删除

    // 地板
    public string groundName;
    public string groundModelFile;
    public GameObject groundObj;
    public int groundRotate;

    public GridData(int x_, int y_)
    {
        objType = ObjType.eot_None;
        isBuilding = false;
        nState = 0;
        x = x_;
        y = y_;
        rotate = 0;
        rotateY = 1;
    }

    public void Clean()
    {
        CleanObj();
        CleanGround();
    }
 
    public void SetData(ObjType objType_, string objName_, int rotate_ = 0)
    {
        objType = objType_;
        isBuilding = ((int)objType&0x8000) != 0;
        objName = objName_;
        nState = 0;
        rotate = rotate_;
    }
    public void SetDataY(ObjType objType_, string objName_, int rotate_ = 1)
    {
        objType = objType_;
        isBuilding = ((int)objType&0x8000) != 0;
        nState = 0;
        objNameY = objName_;
        rotateY = rotate_;
    }

    public void DelObj(int index)
    {
        if(index == 0)
        {
            if(objName == "")
            {
                return;
            }
            if(objNameY == "" || nearList != null)
            {
                CleanObj();
            }
            else
            {
                objName = "";
                rotate = 0;
                GridManager.GetInstance().UpdateAssist(x+1, y);
                UpdateModel();
                GridManager.GetInstance().UpdateAssist(x, y);
            }
        }
        else
        {
            if(objNameY == "")
            {
                return;
            }
            if(objName == "" || nearList != null)
            {
                CleanObj();
            }
            else
            {
                objNameY = "";
                rotateY = 0;
                GridManager.GetInstance().UpdateAssist(x, y+1);
                UpdateModel();
                GridManager.GetInstance().UpdateAssist(x, y);
            }
        }
    }

    public void CleanObj()
    {
        ObjType oldObjType = objType;
        // int oldRotate = rotate;
        
        objType = ObjType.eot_None;
        isBuilding = false;
        objName = "";
        nState = 0;
        rotate = 0;
        objNameY = "";
        rotateY = 1;
        if(nearList != null)
        {
            GridData[] oldNearList = nearList;
            nearList = null;
            for(int i = 0; i < oldNearList.Length; i++)
            {
                if(oldNearList[i] != null) oldNearList[i].CleanObj();
            }
        }

        switch(oldObjType)
        {
            case ObjType.eot_Wall:
            case ObjType.eot_Mine:
            {
                GridManager.GetInstance().ChangeWall(x, y);
            }
            break;
            case ObjType.eot_WoodenWall:
            {
                if(obj)
                {
                    GridManager.GetInstance().UpdateAssist(x+1, y);
                }
                if(objY)
                {
                    GridManager.GetInstance().UpdateAssist(x, y+1);
                }
                UpdateModel();
                GridManager.GetInstance().UpdateAssist(x, y);
            }
            break;
            case ObjType.eot_Other:
            {            
                UpdateModel();
            }
            break;
        }
    }
    public void UpdateModel()
    {
        if(objType == ObjType.eot_None || objType == ObjType.eot_Assist)
        {
            if(obj != null)
            {
                GameObject.DestroyImmediate(obj);
                obj = null;
                modelFile = "";
            }
            if(objY != null)
            {
                GameObject.DestroyImmediate(objY);
                objY = null;
                modelFileY = "";
            }
            return;
        }
        switch(objType)
        {
            case ObjType.eot_Wall:
            case ObjType.eot_Mine:
            {
                WallData newWallData = GridManager.GetInstance().GetWallFile(objName, nState); 
                if(newWallData.fileName != curWallData.fileName)
                {
                    if(obj) GameObject.DestroyImmediate(obj);
                    GameObject temp = (GameObject)Resources.Load(newWallData.fileName);
                    obj = GameObject.Instantiate(temp);
                    obj.transform.parent = GridManager.GetInstance().MyGroundBox.transform;
                    obj.layer = GridManager.mWallLayer;
                    ObjProperty objProp = obj.AddComponent<ObjProperty>();
                    objProp.nX = x;
                    objProp.nZ = y;
                    //obj.transform.rotation;
                    Vector3 pos = Vector3.zero;
                    pos.x = x+0.5f;
                    pos.z = y+0.5f;
                    obj.transform.localPosition = pos;
                    obj.transform.localRotation = new Quaternion(0,1,0, 0)*s_Quaternion[rotate];
                }
                else if(obj != null && newWallData.nRotate != curWallData.nRotate)
                {
                    Vector3 pos = Vector3.zero;
                    pos.x = x+0.5f;
                    pos.z = y+0.5f;
                    obj.transform.localPosition = pos;
                    obj.transform.localRotation = new Quaternion(0,1,0, 0)*s_Quaternion[rotate];
                }
                if(objY != null)
                {
                    GameObject.DestroyImmediate(objY);
                    objY = null;
                }
                curWallData = newWallData;
                modelFile = "";
            }
            break;
            case ObjType.eot_WoodenWall:
            {
                if(objName != "")
                {
                    string ModelFile = GridManager.GetInstance().GetWoodenWallData(objName);
                    if(modelFile != ModelFile)
                    {
                        if(obj != null) GameObject.DestroyImmediate(obj); 
                        GameObject temp = (GameObject)Resources.Load(ModelFile);
                        obj = GameObject.Instantiate(temp);
                        obj.transform.parent = GridManager.GetInstance().MyGroundBox.transform;
                        obj.layer = GridManager.mWallLayer;
                        ObjProperty objProp = obj.AddComponent<ObjProperty>();
                        objProp.nX = x;
                        objProp.nZ = y;
                        modelFile = ModelFile;
                    }

                    Vector3 pos = Vector3.zero;
                    pos.x = x+1;
                    pos.z = y+0.5f;
                    obj.transform.localPosition = pos;
                    obj.transform.localRotation = s_Quaternion[rotate];
                }
                else if(obj != null) 
                {
                    modelFile = "";
                    GameObject.DestroyImmediate(obj);
                    obj = null;
                }
                if(objNameY != "")
                {
                    string ModelFile = GridManager.GetInstance().GetWoodenWallData(objNameY);
                    if(modelFileY != ModelFile)
                    {
                        if(objY != null) GameObject.DestroyImmediate(objY);
                        GameObject temp = (GameObject)Resources.Load(ModelFile);
                        objY = GameObject.Instantiate(temp);
                        objY.transform.parent = GridManager.GetInstance().MyGroundBox.transform;
                        objY.layer = GridManager.mWallLayer;
                        ObjProperty objProp = objY.AddComponent<ObjProperty>();
                        objProp.nX = x;
                        objProp.nZ = y;
                        objProp.nIndex = 1;
                        modelFileY = ModelFile;
                    }

                    Vector3 pos = Vector3.zero;
                    pos.x = x+0.5f;
                    pos.z = y+1;
                    objY.transform.localPosition = pos;
                    objY.transform.localRotation = s_Quaternion[rotateY];
                }
                else if(objY != null)
                {
                    modelFileY = "";
                    GameObject.DestroyImmediate(objY);
                    objY = null;
                }
            }
            break;
            case ObjType.eot_Other:
            {
                if(objName != "")
                {
                    OtherData otherData = GridManager.GetInstance().GetOtherData(objName);
                    if(modelFile != otherData.fileName)
                    {
                        if(obj) GameObject.DestroyImmediate(obj);
                        GameObject temp = (GameObject)Resources.Load(otherData.fileName);
                        obj = GameObject.Instantiate(temp);
                        obj.transform.parent = GridManager.GetInstance().MyGroundBox.transform;
                        obj.layer = GridManager.mGoodsLayer;
                        ObjProperty objProp = obj.AddComponent<ObjProperty>();
                        objProp.nX = x;
                        objProp.nZ = y;
                        modelFile = otherData.fileName;
                    }

                    Vector3 pos = Vector3.zero;
                    if(rotate == 0 || rotate == 2)      
                    {
                        pos.x = otherData.nWidth%2 == 1?(float)x+0.5f:(float)x+1;
                        pos.z = otherData.nHeight%2 == 1?(float)y+0.5f:(float)y+1;
                    }
                    else
                    {
                        pos.x = otherData.nHeight%2 == 1?(float)x+0.5f:(float)x+1;
                        pos.z = otherData.nWidth%2 == 1?(float)y+0.5f:(float)y+1;
                    }

                    obj.transform.localPosition = pos;
                    obj.transform.localRotation = obj.transform.rotation*s_Quaternion[rotate];
                }
                else if(obj != null) 
                {
                    modelFile = "";
                    GameObject.DestroyImmediate(obj);
                    obj = null;
                }
                if(objY != null)
                {
                    GameObject.DestroyImmediate(objY);
                    objY = null;
                }
            }
            break;
        }
    }

    

    public void SetGround(string objName, int rotate_ = 1)
    {
        groundName = objName;
        groundRotate = rotate_;
        UpdateGround();
    }

    public void CleanGround()
    {
        groundName = "";
        groundRotate = 0;
        UpdateGround();
    }
    public void UpdateGround()
    {        
        if(groundName != "")
        {
            string fileName = GridManager.GetInstance().GetGroundData(groundName);
            if(groundModelFile != fileName)
            {
                if(groundObj) GameObject.DestroyImmediate(groundObj);
                GameObject temp = (GameObject)Resources.Load(fileName);
                groundObj = GameObject.Instantiate(temp);
                groundObj.transform.parent = GridManager.GetInstance().MyGroundBox.transform;
                groundObj.layer = GridManager.mGroundLayer;
                ObjProperty objProp = groundObj.AddComponent<ObjProperty>();
                objProp.nX = x;
                objProp.nZ = y;
                groundModelFile = fileName;
            }

            Vector3 pos = Vector3.zero;
            pos.x = x+1.0f;
            pos.z = y+1.0f;
            groundObj.transform.localPosition = pos;
            groundObj.transform.localRotation = s_Quaternion[groundRotate];
        }
        else if(groundObj != null) 
        {
            groundModelFile = "";
            GameObject.DestroyImmediate(groundObj);
            groundObj = null;
        }
    }
}